package model;

import interfaces.DataBuffer;
import interfaces.TagDataGetter;
import interfaces.TagDataSetter;

import java.util.Date;

/**
 * @author Manseer Marcel  
 * @author Plavsic Maja
 * @author Pretsch Paul
 */
public class TagData implements TagDataGetter, TagDataSetter, Comparable<TagData> {
	private final String tag;
	private final Date date;
	private int count;
	private boolean finished;
	private boolean exceptionOccured;
	private String message;
	private DataBuffer buffer;

	public TagData(String tag, Date date, DataBuffer buffer)
	{
		this.tag = tag;
		this.date = Tools.TruncateTime(date);
		this.count = 0;
		this.finished = false;
		this.exceptionOccured = false;
		this.message = "";
		this.buffer = buffer;
	}

	/* (non-Javadoc)
	 * @see model.TagDataSetter#setCount(int)
	 */
	@Override
	public void setCount(int count) {
		if ((!this.finished) && (count != this.count)) {
			// remember old count
			int oldCount = this.count; 
			
			this.count = count;
			fireTagDataChanged(oldCount);
		}
	}

	/* (non-Javadoc)
	 * @see model.TagDataGetter#getTag()
	 */
	@Override
	public String getTag() {
		return this.tag;
	}

	/* (non-Javadoc)
	 * @see model.TagDataGetter#getCount()
	 */
	@Override
	public int getCount() {
		return this.count;
	}

	/* (non-Javadoc)
	 * @see model.TagDataGetter#getDate()
	 */
	@Override
	public Date getDate() {
		return this.date;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(TagData o) {
		int result = this.getTag().compareTo(o.getTag());
		if (result == 0) {
			result = this.getDate().compareTo(o.getDate());
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataSetter#setFinished()
	 */
	@Override
	public void setFinished() {
		this.finished = true;
		fireTagDataChanged(this.count);
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataSetter#setExceptionOccured(java.lang.String)
	 */
	@Override
	public void setExceptionOccured(String message) {
		this.exceptionOccured = true;
		this.message = message;
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataGetter#getExceptionOccured()
	 */
	@Override
	public boolean getExceptionOccured() {
		return this.exceptionOccured;
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataGetter#getMessage()
	 */
	@Override
	public String getMessage() {
		return this.message;
	}
	
	/* (non-Javadoc)
	 * @see interfaces.TagDataGetter#fireTagDataChanged()
	 */
	public final void fireTagDataChanged(int oldCount)
	{
		if (buffer != null) {
			buffer.fireTagDataChanged(this, oldCount);
	    }
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataGetter#getFinished()
	 */
	@Override
	public boolean getFinished() {
		return this.finished;
	}

	/* (non-Javadoc)
	 * @see interfaces.TagDataSetter#delete()
	 */
	@Override
	public void delete() {
		buffer.removeData(this.getDate(), this.getTag());
	}
	
}
